package com.weixin.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.weixin.entity.Purchase_People_Tb;

import com.weixin.service.Purchase_People_TbService;
import com.weixin.service.impl.Purchase_People_TbServiceImpl;
import com.weixin.util.Pageuitl;


/**
 * Servlet implementation class PeopleServlet
 */
@WebServlet("/Purchase_People_TbServlet/*")
public class Purchase_People_TbServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       Purchase_People_TbService ps=new Purchase_People_TbServiceImpl();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String sub= request.getPathInfo().substring(1);
		System.out.println(sub);
		if ("listAll".equals(sub)) {
			listAll(request,response);
		}else if("preAdd".equals(sub)) {
			preAdd(request,response);
		}else if("add".equals(sub)) {
			add(request,response);
		}else if("updateDelete".equals(sub)) {
			updateDelete(request,response);
		}else if("preEdit".equals(sub)) {
			preEdit(request,response);
		}else if("edit".equals(sub)) {
			edit(request,response);
		}else if("listByPage".equals(sub)) {
			listByPage(request,response);
		}else if("listAllSearchPagination".equals(sub)) {
			listAllSearchPagination(request,response);
		}
		
	}

	private void listAllSearchPagination(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		
		String search = request.getParameter("search");
		System.out.println(search);
		try {
			//表格数据===：
			List<Purchase_People_Tb> selectAll = ps.getTotalBySearch(search);
			request.setAttribute("selectAll", selectAll);
			request.setAttribute("pageNum", 1);
			request.setAttribute("pageCount", 1);
			request.getRequestDispatcher("/page/user/people/people_list.jsp").forward(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void listByPage(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String pageNum = request.getParameter("pageNum");
		try {
			List<Purchase_People_Tb> selectAll = ps.listByPage(Pageuitl.PAGE_SIZE,Integer.valueOf(pageNum));
			request.setAttribute("selectAll", selectAll);
			
			request.setAttribute("pageCount",Math.ceil(ps.getTotal()*1.0/Pageuitl.PAGE_SIZE));
			request.setAttribute("pageNum", Integer.valueOf(pageNum));		
		
			request.getRequestDispatcher("/page/user/people/people_list.jsp").forward(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void edit(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String id = request.getParameter("purchase_people_id");
		String name= request.getParameter("purchase_name");
		System.out.println(name);
		String brithday = request.getParameter("brithday");
	    System.out.println(brithday);
	    String address = request.getParameter("address");
	    String email = request.getParameter("email");
	    String qq = request.getParameter("qq_num");
	    String weixin = request.getParameter("weixin_num");
	    String remark = request.getParameter("remark");
	    Purchase_People_Tb ppt=new Purchase_People_Tb();
	    ppt.setPurchase_people_id(Integer.valueOf(id));
	    ppt.setPurchase_name(name);
	    try {
			ppt.setBrithday(new SimpleDateFormat("yyyy-MM-dd").parse(brithday));
			ppt.setAddress(address);
			ppt.setEmail(email);
			ppt.setQq_num(Integer.valueOf(qq));
			ppt.setWeixin_num(weixin);
			ppt.setRemark(remark);
			if (ps.update(ppt)) {
				response.getWriter().println(1);
			}else {
				response.getWriter().println(2);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void preEdit(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String id = request.getParameter("purchase_people_id");
		System.out.println(id);
		try {
			Purchase_People_Tb people = ps.byId(Integer.valueOf(id));
			request.setAttribute("people", people);
		
			request.getRequestDispatcher("/page/user/people/people_edit.jsp").forward(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}

	private void updateDelete(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String purchase_people_id = request.getParameter("purchase_people_id");
		System.out.println(purchase_people_id);
		try {
			if (ps.updateDelete(Integer.valueOf(purchase_people_id))) {
				if (ps.selectAll() != null) {
					response.getWriter().println(1);
				}
				
			}else {
				response.getWriter().println(2);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void add(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String name = request.getParameter("name");
		System.out.println(name);
		String brithday = request.getParameter("brithday");
		String address = request.getParameter("address");
		String email = request.getParameter("email");
		String qq = request.getParameter("qq");
		String weixin = request.getParameter("weixin");
		String remark = request.getParameter("remark");
		Purchase_People_Tb ppt=new Purchase_People_Tb();
		ppt.setPurchase_name(name);
		try {
			ppt.setBrithday(new SimpleDateFormat("yyyy-MM-dd").parse(brithday));
			ppt.setAddress(address);
			ppt.setEmail(email);
			ppt.setQq_num(Integer.valueOf(qq));
			ppt.setWeixin_num(weixin);
			ppt.setRemark(remark);
			ppt.setIs_show(1);
			if (ps.insert(ppt)) {
					response.getWriter().println(1);
			}else {
				response.getWriter().println(2);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void preAdd(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		try {
			List<Purchase_People_Tb> addlist = ps.selectAll();
			request.setAttribute("addlist", addlist);
			request.getRequestDispatcher("/page/user/people/people_add.jsp").forward(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void listAll(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		try {
			List<Purchase_People_Tb> selectAll = ps.selectAll();		
		  	System.out.println(selectAll);
			request.setAttribute("selectAll",selectAll);	
			request.getRequestDispatcher("/page/user/people/people_list.jsp").forward(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
